<template>
  <div class="login">
    <van-nav-bar title="面经登录" />

    <van-form class="login-main" @submit="onSubmit">
      <van-cell-group class="login-input" inset>
        <van-field
          v-model="username"
          label="用户名"
          colon center
          name="username"
          :rules="[{ pattern: /^\w{6,10}/, message: '请输入6~10位字符' }]"
          placeholder="请输入用户名"
        />
        <van-field
          type="password"
          label="密 码"
          v-model="password"
          colon 
          name="password"
          :rules="[{ pattern: /^\w{8,12}/, message: '请输入8~12位字符' }]"
          placeholder="请输入密码"
        />
      </van-cell-group>
      <van-button type="warning" native-type="submit">提交</van-button>
    </van-form>
    <div class="login-footer">
      <router-link to="/register">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import {loginAPI  } from "@/api/uesr";
export default {
  name: "App",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
   async onSubmit(values) {
      const result=await loginAPI(values)
        console.log(result);
        this.$toast.success('登录成功')
        if(result.data.code===10000){
          localStorage.setItem('token',result.data.data.token)
          setTimeout(() => {
            this.$router.push({path:'./'})
          }, 1500);
        }

    },
  },

  computed: {},

  components: {},

  beforeCreate() {},

  mounted() {},

  updated() {},

  watch: {},
};
</script>

<style scoped lang=less>
.login-main {
  display: flex;
  width: 320px;
  padding: 10px;
  margin: 0 auto;
  align-items: center;
  flex-direction: column;
  padding-top: 20px;

  .van-cell {
    background-color: rgb(242, 242, 242);
  }
  button {
    width: 100px;
    margin-top: 20px;
    height: 35px;
  }
}

.login-footer {
  display: flex;
  justify-content: flex-end;
  a {
    font-size: 12px;
    margin-right: 10px;
    color: #ff976a;
  }
}
</style>